package com.aorise.utils;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.text.StringEscapeUtils;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * @Auther: zhouhao
 * @Date: 2019/7/17
 * @Description:
 */
@Slf4j
public class HttpUtils {

    public static String httpGet(String url,Map param){
        RestTemplate restTemplate=new RestTemplate();
        String body = restTemplate.getForEntity(url, String.class,param).getBody().toString();
        body = getJson(body);
        return body;
    }

    public static String httpPost(String url, MultiValueMap<String, Object> param){
        RestTemplate restTemplate=new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<MultiValueMap<String, Object>>(param,headers);
        String body = restTemplate.postForEntity(url, httpEntity, String.class).getBody();
        body = getJson(body);
        return body;
    }

    public static String httpPut(String url, MultiValueMap<String, Object> param){
        RestTemplate restTemplate=new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        HttpEntity<MultiValueMap<String, Object>> entity = new HttpEntity<MultiValueMap<String, Object>>(param,headers);
        String body = restTemplate.exchange(url, HttpMethod.PUT, entity,String.class).getBody();
        body = getJson(body);
        return body;
    }

    private static String getJson(String body) {
        log.info("返回的参数为 : {}",body);
        JSONObject jsonObject = JSONObject.parseObject(body);
        String json2 = (String) jsonObject.get("json2");
        /*监控箱返回的内容为空的时候返回-1*/
        if(json2.equals("-1")){
            return "-1";
        }
        /*CMD判断不正常为-2*/
        if(json2.equals("-2")){
            return "-2";
        }
        body= StringEscapeUtils.unescapeJava(json2);
//        int start = getIndex(json2, 2, "\\{");
////        int end = getIndex(body, 1, "\\}");
//        int end =json2.lastIndexOf("}");
//        body =body.substring(start,end+1);
        JSONObject jsonObject1 = JSONObject.parseObject(body);
        System.out.println(jsonObject1);
        return body.toString();
    }

    /**
     * @param string 字符串
     * @param i      第i次出现
     * @param str    子字符串
     * @return
     */
    private static int getIndex(String string, int i, String str) {
        //这里是获取  str  第 i 次出现的下标
//        Matcher slashMatcher = Pattern.compile("/").matcher(string);
        Matcher slashMatcher = Pattern.compile(str).matcher(string);
        int mIdx = 0;
        while (slashMatcher.find()) {
            mIdx++;
            //当 str 第 i 次出现的位置
            if (mIdx == i) {
                break;
            }
        }
        return slashMatcher.start();
    }

}
